import { ShapeType, Shape, GraphModel } from "@/mdesign-lib/mdesign-graph.umd.js";
import { locale, Action, ActionKey, GraphNode, shapeService, modelService, loading, ModelValidateDegree, CheckElementsByRulesDto, GraphTab, rmi } from "@/mdesign-lib/mdesign-app.umd.js";

export class ValidateDiagramAction extends Action {
  keyName = ActionKey.ValidateDiagram;
  desc = "校验图表";
  onTreeMenuClick(graphNode) {
    if (!graphNode) return;
    this.run(graphNode.id);
  }
  onToolbarClick(projectId: string, graph, tab) {
    this.run(tab.modelId);
  }
  async run(modelId: string) {
    const project = app.activeProject;
    if (!project) return;
    loading.popup(locale.t("校验中") + "...");
    const diagramShape = await shapeService.getDiagramAllShape(project.id, modelId);
    if (!diagramShape || diagramShape.length === 0) {
      loading.close();
      return;
    }
    const filteredShapes: Shape[] = diagramShape.filter(
      shape =>
        shape.shapeType !== ShapeType.Compartment && shape.shapeType !== ShapeType.Diagram && shape.shapeType !== ShapeType.MovableLabel
    );
    const ruleInstanceIds = await modelService.queryValidationRuleInstanceIds({
      projectId: app.activeProjectId,
      severity: ModelValidateDegree.ERROR
    });
    const params = {
      projectId: app.activeProjectId,
      ruleInstanceIds,
      excludeReadOnly: false,
      instanceIds: filteredShapes.map(item => item.modelId),
      isCheckChildren: false,
      severity: ModelValidateDegree.ERROR
    } as CheckElementsByRulesDto;
    await rmi.invokeAction(ActionKey.checkElementsByRules, [JSON.parse(JSON.stringify(params))]);
    loading.close();
  }
}
